package org.cug.controller;

import lombok.extern.slf4j.Slf4j;
import org.cug.pojo.ForeignClass;
import org.cug.pojo.Result;
import org.cug.service.ForeignclassService;
import org.cug.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/foreign-class")
/**
 * 该控制器用于处理外教课相关的代码的增删改查，操纵的数据库为foreign_class
 * state:已测验
 */
public class ForeignclassController {

    @Autowired
    private ForeignclassService foreignclassService;
    //查询数据列表
    @GetMapping
    public Result list(@RequestBody(required = false) Integer semester){
        log.info("查询所有外教课信息");
        //本地线程获取
        Map<String,Object> claims= ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("id");
        List<ForeignClass> foreignclasses=foreignclassService.list(userId,semester);
        return Result.success(foreignclasses);
    }
    //添加数据
    @PostMapping
    public Result add(@RequestBody ForeignClass foreignclass){
        log.info("增加外教课信息{}",foreignclass);
        Map<String,Object> claims= ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("id");
        foreignclass.setUserId(userId);
        foreignclassService.add(foreignclass);
        return Result.success();
    }
    //根据id进行删除
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("删除外教课信息{}",ids);
        foreignclassService.delete(ids);
        return Result.success();
    }
    //根据id进行查询
    @GetMapping("/{id}")
    public Result selectid(@PathVariable Integer id) {
        log.info("查询id{}",id);
        ForeignClass foreignclass=foreignclassService.selectid(id);
        return Result.success(foreignclass);
    }
    //根据id进行修改
    @PutMapping
    public Result alterbyid(@RequestBody ForeignClass foreignclass){
        log.info("修改外教课信息{}",foreignclass);
        foreignclassService.alterbyid(foreignclass);
        return Result.success();
    }
}
